import { message, notification } from 'antd';

const databasePrefix = 'http://localhost:10001';
const pagination = {
    currentPage: 1,
    pageSize: 20,
};
export const axhttp = {
    /** 重写登录 */
    login() {
        window.location.href = '/#/login';
    },

    checkIsLogin() {
        const timeStamp = localStorage.getItem('is_login_timestamp');
        /** 缓存时间 */
        const catcheTime = 1000 * 60 * 120;
        if (!(timeStamp && parseInt(timeStamp) + catcheTime > Date.now())) {
            axhttp.login();
        }
        else {
            /** 更新时间戳 */
            localStorage.setItem('is_login_timestamp', Date.now())
        }
    },

    async postFormXhr(url, data) {
        axhttp.checkIsLogin();

        return new Promise((resolve, reject) => {
            let dataArr = [];
            for (let k in data) {
                data[k] && dataArr.push(k + '=' + data[k]);
            }

            const xhr = new XMLHttpRequest();
            xhr.open('post', databasePrefix + url, true);
            xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
            // xhr.withCredentials = true;
            xhr.send(dataArr.join('&'));
            xhr.onreadystatechange = () => {
                if (xhr.readyState == 4) {
                    if (xhr.status == 200) {
                        var res = JSON.parse(xhr.responseText);
                        if (res.success === true) {
                            return resolve(res);
                        } else {
                            notification.error({
                                message: res.message,
                            });
                        }
                    }
                }
            };
        });
    },

    async getListXhr(url, data) {
        axhttp.checkIsLogin();

        let dataArr = [];
        const params = {
            ...pagination,
            ...data,
        };

        for (let k in params) {
            params[k] && dataArr.push(k + '=' + params[k]);
        }

        return new Promise((resolve, reject) => {
            const xhr = new XMLHttpRequest();
            // xhr.withCredentials = true;
            xhr.open('get', databasePrefix + url + '?' + dataArr.join('&'), true);
            xhr.send();
            xhr.onreadystatechange = () => {
                if (xhr.readyState == 4) {
                    this.progress = 0;

                    if (xhr.status == 200) {
                        var res = JSON.parse(xhr.responseText);
                        if (res.success === true) {
                            return resolve({
                                ...res,
                                pagination: {
                                    current: params.currentPage,
                                    pageSize: params.pageSize,
                                    total: res.total,
                                },
                            });
                        } else {
                            notification.error({
                                message: res.message,
                            });
                        }
                    }
                }
            };
        });
    },

    async getXhr(url, data = {}) {
        axhttp.checkIsLogin();
        let dataArr = [];
        const params = {
            ...data,
        };
        for (let k in params) {
            params[k] && dataArr.push(k + '=' + params[k]);
        }

        return new Promise((resolve, reject) => {
            const xhr = new XMLHttpRequest();
            // xhr.withCredentials = true;
            xhr.open('get', databasePrefix + url + '?' + dataArr.join('&'), true);
            xhr.send();
            xhr.onreadystatechange = () => {
                if (xhr.readyState == 4) {
                    if (xhr.status == 200) {
                        var res = JSON.parse(xhr.responseText);
                        if (res.success === true) {
                            return resolve(res);
                        } else {
                            notification.error({
                                message: res.message,
                            });
                        }
                    }
                }
            };
        });
    },

    async loginXhr(url, data) {
        let dataArr = [];
        const params = {
            ...pagination,
            ...data,
        };
        for (let k in params) {
            params[k] && dataArr.push(k + '=' + params[k]);
        }

        return new Promise((resolve, reject) => {
            const xhr = new XMLHttpRequest();
            // xhr.withCredentials = true;
            xhr.open('get', databasePrefix + url + '?' + dataArr.join('&'), true);
            xhr.send();
            xhr.onreadystatechange = () => {
                if (xhr.readyState == 4) {
                    this.progress = 0;
                    if (xhr.status == 200) {
                        var res = JSON.parse(xhr.responseText);
                        if (res.success === true) {
                            return resolve({
                                ...res,
                                pagination: {
                                    current: params.currentPage,
                                    pageSize: params.pageSize,
                                    total: res.total,
                                },
                            });
                        } else {
                            notification.error({
                                message: res.message,
                            });
                        }
                    }
                    else if (xhr.status === 0) {
                        message.error('连接服务器失败！');
                    }
                }
            };
        });
    },

    async registerXhr(url, data) {
        return new Promise((resolve, reject) => {
            let dataArr = [];
            for (let k in data) {
                data[k] && dataArr.push(k + '=' + data[k]);
            }

            const xhr = new XMLHttpRequest();
            xhr.open('post', databasePrefix + url, true);
            xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
            // xhr.withCredentials = true;
            xhr.send(dataArr.join('&'));
            xhr.onreadystatechange = () => {
                if (xhr.readyState == 4) {
                    if (xhr.status == 200) {
                        var res = JSON.parse(xhr.responseText);
                        if (res.success === true) {
                            return resolve(res);
                        } else {
                            notification.error({
                                message: res.message,
                            });
                        }
                    }
                }
            };
        });
    },
};
